package cn.lzx.controller;




import cn.lzx.domain.JsonResult;
import cn.lzx.domain.MovieTicket;
import cn.lzx.dto.UserMovieDto;
import cn.lzx.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/movie")
public class MovieTicketController {

    @Autowired
    private MovieService movieService;

    @GetMapping("/findById/{id}")
    public JsonResult findById(@PathVariable("id") Long id){
        try {
            MovieTicket movieTicket = movieService.findById(id);
            JsonResult jsonResult = new JsonResult();
            jsonResult.setObject(movieTicket);
            return jsonResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"查询电影票失败");
        }
    }

    @GetMapping("/getTicket/{userId}")
    public JsonResult getTicket(@PathVariable("userId") Long userId){
        try {
            UserMovieDto userMovieDto = movieService.getTicket(userId);
            JsonResult jsonResult = new JsonResult();
            jsonResult.setObject(userMovieDto);
            return jsonResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"通过用户查询电影票失败");
        }
    }



    @RequestMapping(value="/buyTicket",method= RequestMethod.POST)
    public JsonResult buyTicket(@RequestBody UserMovieDto UserMovieDto){
        try {
            movieService.buyTicket(UserMovieDto);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"购买电影票失败");
        }

    }



}
